package com.abc.robot01.activity.compose

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.core.FastOutSlowInEasing
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.abc.robot01.R
import com.abc.robot01.activity.MyFont_Size

class TextFieldDemo : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {

            var largeSize by remember { mutableStateOf(true) }


            Box(
                modifier = Modifier.fillMaxSize()
            ) {
                // 背景图片
                Image(
                    painter = painterResource(id = R.drawable.z1), // 替换为你的图片资源ID
                    contentDescription = null,
                    contentScale = ContentScale.Crop, // 使图片填充整个背景
                    modifier = Modifier.matchParentSize() // 使图片铺满整个Box
                )
            val scrollState = rememberScrollState()
            Column(
                modifier = Modifier
                    .verticalScroll(scrollState)
                    .fillMaxSize()
                    .offset(x = 20.dp)
            ) {

                Spacer(modifier = Modifier.height(20.dp))
//                SimpleFilledTextFieldSample(largeSize)
                Spacer(modifier = Modifier.height(2.dp))
                Button(onClick = { largeSize=largeSize.not() }) {
                    Text(text = "111")

                }
//                SimpleTextField()
                Spacer(modifier = Modifier.height(2.dp))

//                CustomStyledTextField(size= DpSize(200.dp,50.dp),placeHolder="你好"){
//                }

                CustomTextFieldListDemo()

//                TextFieldWithPlaceholder()
                Spacer(modifier = Modifier.height(2.dp))
//                TextFieldWithPlaceholder2()
                Spacer(modifier = Modifier.height(2.dp))
//                TextFieldWithPlaceholder3()
                Spacer(modifier = Modifier.height(2.dp))
//                RoundedCornerTextField()
                Spacer(modifier = Modifier.height(2.dp))
//                RoundedCornerTextField2()
                Spacer(modifier = Modifier.height(2.dp))
//                RoundedCornerTextField3()
                Spacer(modifier = Modifier.height(2.dp))
//                RoundedCornerTextField4()
                Spacer(modifier = Modifier.height(2.dp))
                Spacer(modifier = Modifier.height(2.dp))
                Spacer(modifier = Modifier.height(2.dp))
                Spacer(modifier = Modifier.height(2.dp))

            }}
        }
    }
}


@Composable
fun CustomTextField(text: MutableState<String>) {
    TextField(
        singleLine = true,
        value = text.value,
        onValueChange = { newText -> text.value = newText },
        label = { Text("Input") },
        modifier = Modifier.padding(8.dp)
    )
}


@Composable
fun CustomTextFieldListDemo() {
    // 初始化 MutableState 列表
    val texts = remember {
        List(5) { mutableStateOf("") }  // 创建5个 MutableState<String>
    }

    Column(Modifier.padding(16.dp)) {
        Text("Entered Texts: ${texts.joinToString { it.value }}")
        Spacer(modifier = Modifier.height(16.dp))
        // 遍历 texts 列表，每个 text 传递给 CustomTextField
        texts.forEach { textState ->
            CustomTextField(text = textState)
        }

        // 显示所有输入的文本


    }
}


@Composable
fun SimpleFilledTextFieldSample(largeSize: Boolean) {
    // Animate the size (dp) value
    val size by animateDpAsState(
        targetValue = if (largeSize) 50.dp else 33.dp,
        animationSpec = tween(
            durationMillis = 2000, // animation duration
            easing = FastOutSlowInEasing // easing function
        )
    )

    var text by remember { mutableStateOf("Hello") }
    TextField(
        modifier = Modifier
            .size(374.dp/3.5f, size)
            .background(Color.LightGray), // 设置背景颜色
        textStyle = LocalTextStyle.current.copy(
            fontSize = MyFont_Size, // 设置文字大小
            color = Color.Black // 设置文字颜色
        ),
        colors = TextFieldDefaults.colors(
            focusedIndicatorColor = Color.Transparent,
            unfocusedIndicatorColor = Color.Transparent,
            disabledIndicatorColor  = Color.Transparent ,
        ),
        shape = RoundedCornerShape(4.dp), // 设置圆角
        singleLine = true,
        value = text,
        onValueChange = { text = it },
        placeholder = {
            Text(text = "Enter your text here")  // 设置占位符文本
        },
    )
}

